/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.site.console.action;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.cache.BaseDictCache;
import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.base.BaseDict;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.cud.BaseDictCUDService;
import com.us.usws.service.cud.BaseVideoCatCUDService;
import com.us.usws.service.ro.BaseDictROService;
import com.us.usws.service.ro.BaseVideoCatROService;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.DataPageUtil;
import com.us.usws.support.uitls.JSONDataUtil;
import com.us.usws.support.uitls.Order;
import com.us.usws.support.uitls.SiteWebContext;
import com.us.usws.support.uitls.WebContext;

/**
 * Struts2 action
 * @author robot
 */
public class BaseDictAction extends BasePageSearchAction<BaseDict> {

	private static final long serialVersionUID = 1L;

	private BaseDict baseDict;

	@Inject
	private BaseDictROService baseDictROService;

	@Inject
	private BaseDictCUDService baseDictCUDService;
	
	@Inject
	private BaseVideoCatROService baseVideoCatROService;
	
	@Inject
	private BaseVideoCatCUDService baseVideoCatCUDService;

	private String uids;
	
	private String domain;
	
	private List<String> rateCatUids;
	
	private String provinceCode;
	
	private String cityCode;
	
	private String eduStageCode;
	
	

	/**
	 * ajax load the list of BaseDict
	 */
	public void ajaxLoadBaseDicts() {
		createDataPageUtil();
		dataPageUtil.setOrder(Order.desc("create_date"));
		baseDictROService.loadBaseDictsByPage(dataPageUtil);
		getOut().println(JSONDataUtil.extJsonSupportPage(dataPageUtil.getResult(), dataPageUtil.getRowCount()));
	}
	
	/**
	 * ajax load domain dicts
	 */
	public void ajaxLoadDomainDicts() {
		getOut().println(JSONDataUtil.jsonStringfromObject(baseDictROService.getDictByDomain(domain)));
	}
	
	/**
	 * ajax load provinces
	 */
	public void ajaxLoadProvinces() {
		BaseDictCache dictCache = CacheManager.getDictCache();
		getOut().println(JSONDataUtil.jsonStringfromObject(dictCache.getProvinces()));
	}
	
	/**
	 * ajax load citys
	 */
	public void ajaxLoadCitys() {
		BaseDictCache dictCache = CacheManager.getDictCache();
		getOut().println(JSONDataUtil.jsonStringfromObject(dictCache.getCities(provinceCode)));
	}
	
	/**
	 * ajax load districts
	 */
	public void ajaxLoadDistricts() {
		BaseDictCache dictCache = CacheManager.getDictCache();
		getOut().println(JSONDataUtil.jsonStringfromObject(dictCache.getDistrict(cityCode)));
	}
	
	public void ajaxLoadGrades() {
		BaseDictCache dictCache = CacheManager.getDictCache();
		getOut().println(JSONDataUtil.jsonStringfromObject(dictCache.getSchoolGrade(eduStageCode)));
	}

	/**
	 * ajax update BaseDict
	 */
	public void ajaxUpdateBaseDict() {
		if (baseDict != null) {
			Date now = new Date(System.currentTimeMillis());
			WebContext wc = new SiteWebContext(getRequest(), getResponse());
			DOUser user = wc.getUser();
			String uid = baseDict.getUid();
			if (StringUtils.isNotBlank(uid)) {
				baseDict.setLastUpdByUid(user.getUid());
				baseDict.setLastUpdDate(now);
				baseDictCUDService.updateBaseDict(baseDict);
			} else {
				baseDict.setCreateByUid(user.getUid());
				baseDict.setCreateDate(now);
				baseDictCUDService.addBaseDict(baseDict);
			}
			
			/*
			 * when update video type, and do it
			 */
			if(SiteConstants.DICT_DOMAIN_VIDEO_TYPE.equals(domain)) {
				baseVideoCatCUDService.addRelatedVideoCat(rateCatUids, baseDict.getSepId());
			}
			CacheManager.getDictCache().refresh();
			getOut().print(true);
		} else {
			getOut().print(false);
		}
	}

	/**
	 * ajax delete user.
	 */
	public void ajaxDeleteBaseDict() {
		baseDictCUDService.deleteBaseDictMore(uids);
		CacheManager.getDictCache().refresh();
	}
	
	public void setBaseDict(BaseDict baseDict) {
		this.baseDict = baseDict;
	}

	public BaseDict getBaseDict() {
		return baseDict;
	}

	public DataPageUtil<BaseDict> getDataPageUtil() {
		return dataPageUtil;
	}

	public void setDataPageUtil(DataPageUtil<BaseDict> dataPageUtil) {
		this.dataPageUtil = dataPageUtil;
	}

	public String getUids() {
		return uids;
	}

	public void setUids(String uids) {
		this.uids = uids;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public List<String> getRateCatUids() {
		return rateCatUids;
	}

	public void setRateCatUids(List<String> rateCatUids) {
		this.rateCatUids = rateCatUids;
	}

	public String getProvinceCode() {
		return provinceCode;
	}

	public void setProvinceCode(String provinceCode) {
		this.provinceCode = provinceCode;
	}

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}

	public String getEduStageCode() {
		return eduStageCode;
	}

	public void setEduStageCode(String eduStageCode) {
		this.eduStageCode = eduStageCode;
	}
	
}